package com.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.*;

/**
 * @author cxt
 * @date 2020/9/9
 * 设置静态文件地址
 */
@Component
public class WebConfigImpl implements WebMvcConfigurer {

    @Value("${filePath}")
    private String filePath;

    // 配置跨域
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") // 所有资源访问
                .allowedOrigins("allowedOriginPatterns") // 来源
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS").maxAge(3600).allowedHeaders("*").allowCredentials(true); // 允许证书
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        // 设置上传的文件地址
        registry.addResourceHandler("/file/**").addResourceLocations("file:" + filePath);
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("login");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册TestInterceptor拦截器
        InterceptorRegistration registration = registry.addInterceptor(new AdminInterceptor());
        registration.addPathPatterns("/api/yibao/**");                      //所有路径都被拦截
        //添加不拦截路径,多个之间用逗号分割
        registration.excludePathPatterns("/api/yibao/login");
    }
}
